<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-touch-fullscreen" content="yes">
</head>
<body>
<form id="t">
    <input name="test" value='1'/>
    <input name="test2" value='2'/>
    <input name="testFile" type="file"/>
</form>
<script src="/kissy/build/seed.js"></script>
<script src="/kissy/src/package.js"></script>
<script>
    document.domain = location.hostname;
    KISSY.use("core", function (S) {
        var Dom = S.DOM, Event = S.Event, UA = S.UA;
        var doFormUpload = function (o, ps, url) {
            var id = S.guid("form-upload-");
            var frame = document.createElement('iframe');
            frame.id = id;
            frame.name = id;
            frame.className = 'ke-hidden';
            frame.src = Dom.getEmptyIframeSrc();
            S.log("doFormUpload : " + frame.src);
            document.body.appendChild(frame);
            if (UA.ie) {
                document['frames'][id].name = id;
            }
            var form = o.form,
                    buf = {
                        target: Dom.attr(form, "target"),
                        method: Dom.attr(form, "method"),
                        encoding: Dom.attr(form, "encoding"),
                        enctype: Dom.attr(form, "enctype"),
                        "action": Dom.attr(form, "action")
                    };
            Dom.attr(form, {
                target: id,
                "method": "post",
                enctype: 'multipart/form-data',
                encoding: 'multipart/form-data'
            });
            if (url) {
                Dom.attr(form, "action", url);
            }
            var hiddens, hd;
            if (ps) { // add dynamic params
                hiddens = [];
                for (var k in ps) {
                    hd = document.createElement('input');
                    hd.type = 'hidden';
                    hd.name = k;
                    hd.value = ps[k];
                    form.appendChild(hd);
                    hiddens.push(hd);
                }
            }
            function cb() {
                var r = {  // bogus response object
                    responseText: '',
                    responseXML: null
                };

                r.argument = o ? o.argument : null;

                try { //
                    var doc;

                    if (UA.ie) {
                        doc = frame.contentWindow.document;
                    } else {
                        doc = (frame.contentDocument || window.frames[id].document);
                    }

                    if (doc && doc.body) {
                        r.responseText = S.trim(Dom.text(doc.body));
                    }
                    if (doc && doc['XMLDocument']) {
                        r.responseXML = doc['XMLDocument'];
                    } else {
                        r.responseXML = doc;
                    }

                }
                catch (e) {
                    // ignore
                    // 2010-11-15 由于外边设置了document.domain导致读不到数据抛异常
                    S.log("after data returns error ,maybe domain problem:");
                    S.log(e, "error");
                }

                Event.remove(frame, 'load', cb);
                o.callback && o.callback(r);

                setTimeout(function () {
                    Dom.remove(frame);
                }, 100);

            }

            setTimeout(function () {

                Event.on(frame, 'load', cb);

                form.submit();

                Dom.attr(form, buf);

                if (hiddens) { // remove dynamic params
                    for (var i = 0, len = hiddens.length; i < len; i++) {
                        Dom.remove(hiddens[i]);
                    }
                }
            }, 10);
            return frame;
        };

        doFormUpload({
                    form: S.get('#t'),
                    callback: function (data) {
                        alert(data.responseText);
                    }
                }, {
                    s: 1
                },
                'http://' + location.hostname + ':' + SERVER_CONFIG.ports[1] + '/' +
                        'kissy/src/io/tests/subdomain/upload.jss');
    });
</script>
</body>
</html>